<?php
class Default_AbcController extends Zend_Controller_Action {
	public function init()
	{
		$module= strtolower(str_replace('-','',$this->_request->getModuleName()));
		
		//改变模板文件位置
		$this->view->setScriptPath("./$module/views/");
		//模板文件访问路径
// 		$tpl = $this->view->getScriptPaths();
// 		$this->view->tplpath = $this->view->domain.'/'.str_replace('\\','/',$tpl[0]);
// 		print_r($tpl);
// 		exit;
		
	}
	public function preDispatch() {
		
		
	}
	
	public function indexAction() {
		$layout = Zend_Registry :: get('layout');
		
		$result = Default_Service_Customer::getAll();
		print_r($result);
		$this->view->result = $result;	
//		$layout->content = Zend_Registry::get('CffView')->render();
		
//		echo $layout->render();
	}
	
	
	public function leftMenuAction() {
		$session = new Zend_Session_Namespace ( 'userAuth' );
		
		$user_id = $session->userID;
		$mainMenuArr = $um_ur_Arr = $ur_urlArr = array ();
		if ($user_id) {
			$urm_rows = User_Service_RightMap::getRightByUserid ( $user_id );
			$urIdArr = array ();
			foreach ( $urm_rows as $key => $val ) {
				$urIdArr [] = $val ['ur_id']; // 用户的所有right ID
			}
			$ur_rows = User_Service_Right::getByCond ( array (), '*', 'ur_id', 0, 0 );
			
			// user_menu 相当于大类别，user_right为小类别
			foreach ( $ur_rows as $key => $val ) {
				if (in_array ( $val ['ur_id'], $urIdArr )) {
					$um_ur_Arr [$val ['um_id']] [$val ['ur_id']] = $val ['ur_name']; // 菜单ID对应的right名称
					$ur_urlArr [$val ['ur_id']] = $val ['ur_url']; // right ID对应的ur_url
				}
			}
			unset ( $um_ur_Arr [0] ); // default
			
			$umIdArr = array_keys ( $um_ur_Arr ); // 菜单ID数组
			foreach ( $umIdArr as $key => $val ) {
				$umRow = User_Service_Menu::getByUmid ( $val );
				$mainMenuArr [$val] = $umRow ['um_title'];
			}
		
		}
		$this->view->um_ur_Arr = $um_ur_Arr;
		$this->view->ur_urlArr = $ur_urlArr;
		$this->view->mainMenuArr = $mainMenuArr;
		echo $this->view->render ( 'default/index/leftmenu.tpl' );
	}
	
	public function nopermissionAction() {
		$session = new Zend_Session_Namespace ( 'userAuth' );
		if (isset ( $session->userID )) {
			$this->view->username = $session->username;
		}
		echo $this->view->render ( 'default/index/nopermission.tpl' );
	}
	
	public function topAction() {
		
		echo $this->view->render ( 'default/index/top.tpl' );
	
	}
	public function loginAction() {
		$session = new Zend_Session_Namespace ( 'userAuth' );
		if ($session->isLogin) {
			$this->_redirect ( '/' );
		}
		$errMsg = '';
		$this->view->errMsg = $errMsg;
		// for($a=1;$a<10;$a++){
		// file_put_contents("./html/$a.html",$this->view->render('user/user/login.tpl'));
		// }
		echo $this->view->render ( 'user/user/login.tpl' );
	}
	
	public function logoutAction() {
		$session = new Zend_Session_Namespace ( 'userAuth' );
		$session->unsetAll ();
		session_destroy ();
		// $_SESSION = array();//将所有在登录时保存的SESSION置空
		// unset($_SESSION);
		$errMsg = '';
		$this->view->errMsg = $errMsg;
		echo $this->view->render ( 'user/user/login.tpl' );
	}
	
	public function verifycodeAction() {
		$verifyCode = new Common_Service_Verifycode ();
		$verifyCode->set_sess_name ( 'UserVerifyCode' );
		echo $verifyCode->render ();
	}
	
	public function titleAction() {
		echo $this->view->render ( 'default/index/title.tpl' );
	}
	
	public function authenticateAction() {
		$ip = User_Service_User::getRealIp ();
		$log = array ();
		$errMsg = '';
		if ($this->_request->isPost ()) {
			$username = $_POST ['username'];
			$password = $_POST ['password'];
			$verifyCode = $_POST ['verifyCode'];
			// 数据检查
			$sverifyCode = new Common_Service_Verifycode ();
			$sverifyCode->set_sess_name ( 'UserVerifyCode' );
			
			if (! $sverifyCode->is_true ( $verifyCode )) { // 设置始终为false，即取消验证码
				$errMsg .= '验证码错误';
				$log ['status'] = 1; // 记录登陆信息状态
			} else { // 验证码正确
				
				$user = User_Service_User::getByLoginId ( $username );
				if (empty ( $user )) {
					$errMsg .= '用户不存在';
					$log ['status'] = 2;
				} else { // 用户存在，继续验证密码
					if (Common_Service_Password::comparePassword ( $password, $user ['passwd'] )) {
						$session = new Zend_Session_Namespace ( 'userAuth' );
						$session->unsetAll ();
						
						// 判断最近一次修改密码是否超过60天
						$date = date ( 'Y-m-d' );
						$user_date = $user ['passwd_update_date'];
						$d1 = strtotime ( $date );
						$d2 = strtotime ( $user_date );
						$Days = round ( ($d1 - $d2) / 3600 / 24 ); // 计算相隔天数
						
						if (($Days >= 60) || $user ['passwd_update_date'] == '') {
							$msgSession = new Zend_Session_Namespace ( 'message' );
							$msgSession->msg = '温馨提示：您已超过60天未修改登录密码，请修改后重新登录！';
							$session->userID = $user ['user_id'];
							$this->_redirect ( '/user/user/modifypassword' );
							exit ();
						}
						if ($user ['status'] == '1') { // status状态，0不激活，1激活
						                              // login
							$session->userID = $user ['user_id'];
							$session->username = $user ['login_id'];
							$session->name = $user ['name'];
							$session->last_update = $user ['last_update'];
							$session->login_date = date ( 'Y-m-d' );
							$session->isLogin = true;
							unset ( $user ['passwd'] );
							$session->userInfo = $user;
							
							$this->view->user_id = $session->userID;
							$this->view->username = $session->username;
							$this->view->name = $session->name;
							
							$userRow = array ('last_login' => date ( 'Y-m-d H:i:s' ) );
							User_Service_User::update ( $userRow, $session->userID ); // 更新最后登录时间
							$log ['status'] = 5;
							
							$this->_redirect ( "/" ); // 登陆成功
						} else {
							$errMsg .= '帐号被锁定';
							$log ['status'] = 3;
						}
					} else {
						$errMsg .= '密码错误';
						$log ['status'] = 4;
					}
				
				}
			
			}
			
			$session = new Zend_Session_Namespace ( 'userAuth' );
			if (true == $session->isLogin) {
				$this->_redirect ( "/" );
			} else {
				$this->view->errMsg = $errMsg;
				echo $this->view->render ( 'default/index/login.tpl' );
			}
		
		}
	
	}
	public function testAction() {
		// $this->_request->page,$this->_request->pageSize
		
		// echo $this->view->render('default/index/test-pagination.tpl');
		
		echo json_encode ( array ("ask" => true ) );
	}
	
	public function luceneIndexAction() {
		echo "start<br/>";
		$lucene_service = new Common_Service_Lucene ();
		$lucene_service->index ();
		echo "success";
		exit ();
	}
	public function luceneSearchAction() {
		echo "start<br/>";
		$lucene_service = new Common_Service_Lucene ();
		$keys = array ("sp_id" => "404132" );
		$keys = array ();
		$hits = $lucene_service->search ( $keys );
		// print_r($hits->toArray());
		echo "success";
		exit ();
	}
	
	public function adminUiDocAction() {
		echo "<pre>";
		echo htmlspecialchars ( file_get_contents ( "./admin.ui.txt" ) );
		echo "</pre>";
	}

}